package cn.com.dhcc.app.pub.core.service;

import javax.sql.DataSource;

import org.apache.ftpserver.FtpServerConfigurationException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.usermanager.DbUserManagerFactory;

import cn.com.dhcc.turbo.service.exch.ApiUserAuthService;

public class TurboDbUserManagerFactory extends DbUserManagerFactory {

	private ApiUserAuthService apiUserAuthService;
	@Override
	public UserManager createUserManager() {
		 String adminName =  super.getAdminName();
		 String insertUserStmt = getSqlUserInsert();
		 String updateUserStmt = getSqlUserUpdate();
		 String deleteUserStmt = getSqlUserDelete();
		 String selectUserStmt = getSqlUserSelect();
		 String selectAllStmt = getSqlUserSelectAll();
		 String isAdminStmt = getSqlUserAdmin();
		 String authenticateStmt = getSqlUserAuthenticate();
		 DataSource dataSource = getDataSource(); 
		  if (dataSource == null) {
	            throw new FtpServerConfigurationException(
	                    "Required data source not provided");
	        }
	        if (insertUserStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required insert user SQL statement not provided");
	        }
	        if (updateUserStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required update user SQL statement not provided");
	        }
	        if (deleteUserStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required delete user SQL statement not provided");
	        }
	        if (selectUserStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required select user SQL statement not provided");
	        }
	        if (selectAllStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required select all users SQL statement not provided");
	        }
	        if (isAdminStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required is admin user SQL statement not provided");
	        }
	        if (authenticateStmt == null) {
	            throw new FtpServerConfigurationException(
	                    "Required authenticate user SQL statement not provided");
	        }
	        return new TurboDbUserManager(dataSource, selectAllStmt, selectUserStmt, 
	                insertUserStmt, updateUserStmt, deleteUserStmt, authenticateStmt, 
	                isAdminStmt, getPasswordEncryptor(), adminName,apiUserAuthService);
	}
	public ApiUserAuthService getApiUserAuthService() {
		return apiUserAuthService;
	}
	public void setApiUserAuthService(ApiUserAuthService apiUserAuthService) {
		this.apiUserAuthService = apiUserAuthService;
	}
	
}
